package com.muyu.danmo.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.muyu.common.core.domain.Result;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoCommunity;
import com.muyu.danmo.domain.DanmoCommunityFavorite;
import com.muyu.danmo.domain.DanmoTrainingInformation;
import com.muyu.danmo.mapper.DanmoTrainingInformationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.muyu.danmo.mapper.DanmoTrainingFavoriteMapper;
import com.muyu.danmo.domain.DanmoTrainingFavorite;
import com.muyu.danmo.service.IDanmoTrainingFavoriteService;

/**
 * 培训班收藏Service业务层处理
 *
 * @author kunzong
 * @date 2024-11-27
 */
@Service
public class DanmoTrainingFavoriteServiceImpl implements IDanmoTrainingFavoriteService {
    @Autowired
    private DanmoTrainingFavoriteMapper danmoTrainingFavoriteMapper;

    @Autowired
    private DanmoTrainingInformationMapper danmoTrainingInformationMapper;

    /**
     * 查询培训班收藏
     *
     * @param id 培训班收藏主键
     * @return 培训班收藏
     */
    @Override
    public DanmoTrainingFavorite selectDanmoTrainingFavoriteById(Long id) {
        return danmoTrainingFavoriteMapper.selectDanmoTrainingFavoriteById(id);
    }

    /**
     * 查询培训班收藏列表
     *
     * @param danmoTrainingFavorite 培训班收藏
     * @return 培训班收藏
     */
    @Override
    public List<DanmoTrainingFavorite> selectDanmoTrainingFavoriteList(DanmoTrainingFavorite danmoTrainingFavorite) {
        return danmoTrainingFavoriteMapper.selectDanmoTrainingFavoriteList(danmoTrainingFavorite);
    }

    /**
     * 新增培训班收藏
     *
     * @param danmoTrainingFavorite 培训班收藏
     * @return 结果
     */
    @Override
    public int insertDanmoTrainingFavorite(DanmoTrainingFavorite danmoTrainingFavorite) {
        return danmoTrainingFavoriteMapper.insertDanmoTrainingFavorite(danmoTrainingFavorite);
    }

    /**
     * 修改培训班收藏
     *
     * @param danmoTrainingFavorite 培训班收藏
     * @return 结果
     */
    @Override
    public int updateDanmoTrainingFavorite(DanmoTrainingFavorite danmoTrainingFavorite) {
        return danmoTrainingFavoriteMapper.updateDanmoTrainingFavorite(danmoTrainingFavorite);
    }

    /**
     * 批量删除培训班收藏
     *
     * @param ids 需要删除的培训班收藏主键
     * @return 结果
     */
    @Override
    public int deleteDanmoTrainingFavoriteByIds(Long[] ids) {
        return danmoTrainingFavoriteMapper.deleteDanmoTrainingFavoriteByIds(ids);
    }

    /**
     * 删除培训班收藏信息
     *
     * @param id 培训班收藏主键
     * @return 结果
     */
    @Override
    public int deleteDanmoTrainingFavoriteById(Long id) {
        return danmoTrainingFavoriteMapper.deleteDanmoTrainingFavoriteById(id);
    }

    @Override
    public Result isCollect(Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        DanmoTrainingFavorite danmoTrainingFavorite = danmoTrainingFavoriteMapper.selectDanmoCollectMaterialByIdAndUserId(id, loginUser.getUserid());

        if (danmoTrainingFavorite == null) {
            DanmoTrainingFavorite danmoCommunityFavorite1 = danmoTrainingFavoriteMapper.selectDanmoCommunityFavoriteById(id);
            if (danmoCommunityFavorite1 != null) {
                if (danmoCommunityFavorite1.getCreateBy().equals(loginUser.getUserid())) {
                    return Result.error("不能收藏自己的帖子");
                }
            }
            danmoTrainingFavorite = new DanmoTrainingFavorite();
            danmoTrainingFavorite.setUserId(loginUser.getUserid());
            danmoTrainingFavorite.setPostId(id);
            danmoTrainingFavorite.setFavoriteName("true");
            danmoTrainingFavoriteMapper.insertDanmoTrainingFavorite(danmoTrainingFavorite);
            return Result.success(id, "收藏成功");
        } else {
            danmoTrainingFavoriteMapper.deleteDanmoTrainingFavoriteById(id);
            return Result.success(1, "取消收藏成功");
        }
    }

    /**
     * 查询我收藏的培训班列表
     * @param danmoTrainingInformation
     * @return
     */
    @Override
    public List<DanmoTrainingInformation> selectOtherCollectionDanmoCommunityList(DanmoTrainingInformation danmoTrainingInformation) {
        List<DanmoTrainingInformation> danmoTrainingInformations =  new ArrayList<>();
//        先查询出该用户的所有收藏（主要是看收藏的帖子ID）
        List<Long> ids = new ArrayList<>();
        List<DanmoTrainingFavorite> danmoCommunityFavoriteList = danmoTrainingFavoriteMapper.selectDanmoCommunityFavoriteByUserId(danmoTrainingInformation.getUserId());
        if (danmoCommunityFavoriteList.size() == 0) {
            return danmoTrainingInformations;
        } else {
            for (DanmoTrainingFavorite danmoTrainingFavorite : danmoCommunityFavoriteList) {
                Long postId = danmoTrainingFavorite.getPostId();
                ids.add(postId);
            }
            danmoTrainingInformations = danmoTrainingInformationMapper.selectByIds(ids);
            for (DanmoTrainingInformation danmoTrainingFavorite : danmoTrainingInformations) {
                danmoTrainingFavorite.setIsCollect("true");
            }
            return danmoTrainingInformations;
        }
    }
}
